<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="文档名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="[ 'documentName', validatorRules.documentName]" placeholder="请输入文档名称"></a-input>
        </a-form-item>
        <!--<a-form-item label="文档类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
          &lt;!&ndash;<j-dict-select-tag type="list" v-decorator="['documentType']" :trigger-change="true" dictCode="document_type"
                             placeholder="请选择文档类型"/>&ndash;&gt;
          <a-input v-decorator="[ 'documentType', validatorRules.documentType]" placeholder="请选择文档类型" initialValue="0"></a-input>
        </a-form-item>-->
        <a-form-item label="生效时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择生效时间" v-decorator="[ 'startDate', validatorRules.startDate]" :trigger-change="true"
                  style="width: 100%"/>
        </a-form-item>
        <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['state']" :trigger-change="true" dictCode="document_status"
                             placeholder="请选择状态"/>
        </a-form-item>

        <a-form-item label="文档功用描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea v-decorator="['positionDesc']" rows="4" placeholder="请输入文档功用描述"/>
        </a-form-item>
        <a-form-item label="文档资料" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-upload v-decorator="['path']" :trigger-change="true"></j-upload>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

    import {httpAction} from '@/api/manage'
    import pick from 'lodash.pick'
    import JDate from '@/components/jeecg/JDate'
    import JUpload from '@/components/jeecg/JUpload'
    import JDictSelectTag from "@/components/dict/JDictSelectTag"

    export default {
        name: "DocumentManagerModal",
        components: {
            JDate,
            JUpload,
            JDictSelectTag,
        },
        data() {
            return {
                form: this.$form.createForm(this),
                title: "操作",
                width: 800,
                visible: false,
                model: {},
                labelCol: {
                    xs: {span: 24},
                    sm: {span: 5},
                },
                wrapperCol: {
                    xs: {span: 24},
                    sm: {span: 16},
                },

                confirmLoading: false,
                validatorRules: {
                    documentType: {},
                    positionDesc: {},
                    startDate: {},
                    state: {},
                    documentName: {},
                    path: {},
                },
                url: {
                    add: "/financialservices/documentManager/add",
                    edit: "/financialservices/documentManager/edit",
                }

            }
        },
        created() {
        },
        methods: {
            add() {
                this.edit({});
            },
            edit(record) {
                this.form.resetFields();
                this.model = Object.assign({}, record);
                this.visible = true;
                this.$nextTick(() => {
                    this.form.setFieldsValue(pick(this.model, 'documentName','documentType', 'positionDesc', 'startDate', 'state', 'path'))
                })
            },
            close() {
                this.$emit('close');
                this.visible = false;
            },
            handleOk() {
                const that = this;
                // 触发表单验证
                this.form.validateFields((err, values) => {
                    if (!err) {
                        that.confirmLoading = true;
                        let httpurl = '';
                        let method = '';
                        if (!this.model.id) {
                            httpurl += this.url.add;
                            method = 'post';
                        } else {
                            httpurl += this.url.edit;
                            method = 'put';
                        }
                        let formData = Object.assign(this.model, values);
                        console.log("表单提交数据", formData)
                        //添加默认值
                        formData.documentType='3';
                        console.log("表单提交数据", formData)
                        httpAction(httpurl, formData, method).then((res) => {
                            if (res.success) {
                                that.$message.success(res.message);
                                that.$emit('ok');
                            } else {
                                that.$message.warning(res.message);
                            }
                        }).finally(() => {
                            that.confirmLoading = false;
                            that.close();
                        })
                    }

                })
            },
            handleCancel() {
                this.close()
            },
            showGoodsGallery1(record) {
                alert(record + "自定义弹出框")
                this.id = record.id;
                this.visible = true
            },
            popupCallback(row) {
                this.form.setFieldsValue(pick(row, 'documentName', 'positionDesc', 'startDate', 'state', 'path'))
            }

        }
    }
</script>